from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import _createObjectByType
from Acquisition import aq_base

def createTaxonomy(portal):
    request = portal.REQUEST
    portal_workflow = getToolByName(portal, 'portal_workflow')
    request = portal.REQUEST

    ## Deleta pastas ##
    news = getattr(portal, 'news', None)
    if news is not None: 
        portal.manage_delObjects('news')
    events = getattr(portal, 'events', None)
    if events is not None: 
        portal.manage_delObjects('events')

    ## Portal Fundos ##
    portal_fundos = getattr(portal, 'portal_fundos', None)
    if portal_fundos is None:
        portal.invokeFactory('Folder', id='portal_fundos', title='SIGFS')
        portal_fundos = getattr(portal, 'portal_fundos')
        portal_workflow.doActionFor(portal_fundos, 'publish')
        portal_fundos = getattr(portal, 'portal_fundos')

        portal_types = getToolByName(portal, 'portal_types')
        addable_types=['Large Plone Folder']
        portal_types['Plone Site'].allowed_content_types = addable_types

        portal_fundos.invokeFactory('Folder', id='informacoes', title='Informações Gerais')
        portal_fundos.invokeFactory('Folder', id='legislacoes', title='Legislação dos Fundos Setoriais')
        portal_fundos.invokeFactory('Folder', id='chamadas', title='Chamadas Públicas dos Fundos de C&T')
        portal_fundos.invokeFactory('Folder', id='dotacao', title='Dotação Orçamentária e Execução')
        portal_fundos.invokeFactory('Folder', id='documentos', title='Documentos')
        portal_fundos.invokeFactory('Folder', id='agenda', title='Agenda dos Fundos de C&T')
        portal_fundos.invokeFactory('Folder', id='fale-conosco', title='Fale Conosco')
        portal_fundos.invokeFactory('Folder', id='relatorios', title='Relatórios')
	# Formulario de Proposta acao transversal
#        portal_fundos.invokeFactory('Link', id='propostaTransversal', title='Propostas')

        relatorios = getattr(portal_fundos, 'relatorios', None)
        portal_workflow.doActionFor(relatorios, 'hide')

        portal_fundos.invokeFactory('Folder', id='eventos', title='Eventos')
        eventos = getattr(portal_fundos, 'eventos', None)
        eventos.invokeFactory('Window', id='index_html', title='Eventos')
        busca = getattr(eventos, 'index_html', None)
        busca.edit(remoteUrl='http://scf-informatica.no-ip.com:7080/ibictsigfs/buscaEventos.jsp', hide_metadata=1, show_reference=0, catalog_page_content=1)
        if busca is not None:
            portal_workflow.doActionFor(busca, 'publish')

        ## Notícias ##
        if 'noticias' not in portal_fundos.objectIds():
            _createObjectByType('Large Plone Folder', portal_fundos, id='noticias',
                                title='Notícias')
            noticias = getattr(portal_fundos, 'noticias')
            portal_workflow.doActionFor(noticias, 'publish')
            if getattr(noticias, 'lista-noticias', None) is None:
                noticias.invokeFactory('Topic', id='lista-noticias', title='Notícias')
                noticias.setDefaultPage('lista-noticias')
                lista = getattr(noticias, 'lista-noticias', None)
        
                lista.setSortCriterion('created', True)
                type_crit = lista.addCriterion('Type','ATPortalTypeCriterion')
                type_crit.setValue('News Item')
    
            news = getattr(aq_base(portal_fundos), 'noticias')
            portal_types = getToolByName(portal, 'portal_types')
            addable_types = ['News Item']
            if getattr(news.aq_base, 'setConstrainTypesMode', None) is not None:
                news.setConstrainTypesMode(1)
                news.setImmediatelyAddableTypes(addable_types)
                news.setLocallyAllowedTypes(addable_types)

        portal_fundos.invokeFactory('Folder', id='imagens', title='Imagens')
        for id in ['informacoes', 'legislacoes', 'chamadas', 'dotacao', 'documentos', 'agenda', 'fale-conosco', 'eventos']:
            obj = getattr(portal_fundos, id)
            portal_workflow.doActionFor(obj, 'publish')

        portal_fundos = getattr(aq_base(portal), 'portal_fundos')
        addable_types = ['Document', 'Documento', 'Folder', 'Window', 'File', 'Link', 'Ploneboard', 'PloneChat', 'FaqFolder', 'Image']
        if getattr(portal_fundos.aq_base, 'setConstrainTypesMode', None) is not None:
            portal_fundos.setConstrainTypesMode(1)
            portal_fundos.setImmediatelyAddableTypes(addable_types)
            portal_fundos.setLocallyAllowedTypes(addable_types)


    ## Fundos ##
    fundos = getattr(portal, 'fundos', None)
    if fundos is None:
        portal.invokeFactory('Folder', id='fundos', title='Fundos Setoriais')
        fundos = getattr(portal, 'fundos')
        portal_workflow.doActionFor(fundos, 'publish')
        fundos = getattr(portal, 'fundos')

        if not fundos.hasProperty('right_slots'):
            fundos.manage_addProperty('right_slots', \
                                      ['here/portlet_navegacao_portal/macros/portlet', \
                                       'here/portlet_navegacao_fundos/macros/portlet', \
                                       'here/portlet_review/macros/portlet', \
                                       'here/portlet_member/macros/portlet', \
                                       'here/portlet_activeusers21/macros/portlet', \
                                       'here/portlet_message_list21/macros/portlet', \
                                       'here/portlet_recent/macros/portlet',], \
                                      'lines')

        fundos.invokeFactory('Folder', id='ct-aeronautico', title='CT-Aeronáutico')
        fundos.invokeFactory('Folder', id='ct-agronegocio', title='CT-Agronegócio')
        fundos.invokeFactory('Folder', id='ct-amazonia', title='CT-Amazônia')
        fundos.invokeFactory('Folder', id='ct-biotecnologia', title='CT-Biotecnologia')
        fundos.invokeFactory('Folder', id='ct-energ', title='CT-Energ')
        fundos.invokeFactory('Folder', id='ct-espacial', title='CT-Espacial')
        fundos.invokeFactory('Folder', id='ct-hidro', title='CT-Hidro')
        fundos.invokeFactory('Folder', id='ct-info', title='CT-Info')
        fundos.invokeFactory('Folder', id='ct-infra', title='CT-Infra')
        fundos.invokeFactory('Folder', id='ct-mineral', title='CT-Mineral')
        fundos.invokeFactory('Folder', id='ct-petro', title='CT-Petro')
        fundos.invokeFactory('Folder', id='ct-saude', title='CT-Saúde')
        fundos.invokeFactory('Folder', id='ct-transporte', title='CT-Transporte')
        fundos.invokeFactory('Folder', id='ct-transporte-aquaviario', title='CT-Transporte Aquaviário e de Construção Naval')
        fundos.invokeFactory('Folder', id='ct-verde-amarelo', title='CT-Verde Amarelo')
        fundos.invokeFactory('Folder', id='funttel', title='Funttel')
        for id in ['ct-aeronautico', 'ct-agronegocio', 'ct-amazonia', 'ct-biotecnologia', 'ct-energ', \
                   'ct-espacial', 'ct-hidro', 'ct-info', 'ct-infra', 'ct-mineral', 'ct-petro', 'ct-saude', \
                   'ct-transporte', 'ct-transporte-aquaviario', 'ct-verde-amarelo', 'funttel']:
            obj = getattr(fundos, id)
            portal_workflow.doActionFor(obj, 'publish')

            obj.invokeFactory('Folder', id='legislacao-fundo', title='Legislação')
            obj.invokeFactory('Folder', id='estudos', title='Estudos')
            obj.invokeFactory('Folder', id='atas', title='Atas')
            obj.invokeFactory('Folder', id='projetos-apoiados', title='Projetos Apoiados')
            obj.invokeFactory('Folder', id='comite', title='Comitê Gestor')
            obj.invokeFactory('Folder', id='eventos', title='Eventos')
            obj.invokeFactory('EscritorioVirtualComite', id='escritorio-virtual', title='Escritório Virtual')
            for id in ['legislacao-fundo', 'estudos', 'atas', 'projetos-apoiados', 'comite', 'eventos']:
                objetos = getattr(obj, id)
                portal_workflow.doActionFor(objetos, 'publish')

        fundos.invokeFactory('EscritorioVirtualSecretaria', id='ascofs', title='ASCOFS')

        fundos = getattr(aq_base(portal), 'fundos')
        addable_types = ['Document', 'Documento', 'Folder', 'Window', 'File', 'Link', 'EscritorioVirtualComite', 'Ploneboard', 'PloneChat', 'FaqFolder']
        if getattr(fundos.aq_base, 'setConstrainTypesMode', None) is not None:
            fundos.setConstrainTypesMode(1)
            fundos.setImmediatelyAddableTypes(addable_types)
            fundos.setLocallyAllowedTypes(addable_types)
